﻿@using Jx.Toolbox.Extensions
@using JxAudio.TransVo
@using Microsoft.Extensions.Localization
@inject DialogService DialogService
@inject IStringLocalizer<PlayListControl> StringLocalizer
@inject IStringLocalizer<Program> CommonStringLocalizer

<ListGroup TItem="TrackVo" Items="Tracks" 
           style="height: 20rem;min-width: 300px">
    <HeaderTemplate>
        <div class="list-group-header">
            <div class="flex-fill">@StringLocalizer["Playlist"]</div>
            <Button Color="Color.Danger" Icon="fa fa-plus" Size="Size.ExtraSmall" OnClick="CreatePlaylist">@StringLocalizer["CreatePlaylist"]</Button>
        </div>
    </HeaderTemplate>
    <ItemTemplate>
        <div class="list-group-item" style="@CreateStyle(context)">
            <div class="flex-1" @onclick="() => SelectChanged(context)">
                @if (context == SelectedTrack)
                {
                    <i class="fas fa-play"></i>
                }
                else
                {
                    var index = Tracks?.IndexOf(context);
                    if (index is null or -1)
                    {
                        index = 0;
                    }
                    index++;

                    @index
                }
            </div>
            <div class="flex-fill" @onclick="() => SelectChanged(context)">
                @($"{(context.Title.IsNullOrEmpty() ? CommonStringLocalizer["UnknownTitle"] : context.Title)} - {(context.Artists is {Count:> 0} ? string.Join(',', context.Artists.Select(x => x.Name)) : CommonStringLocalizer["UnknownArtists"])}")
            </div>
            <LinkButton Color="Color.None" Icon="far fa-trash-alt" OnClick="() => DeleteTrack(context)"></LinkButton>
        </div>
    </ItemTemplate>
</ListGroup>

@code {
    [Parameter]
    public List<TrackVo>? Tracks { get; set; }

    [Parameter]
    public TrackVo? SelectedTrack { get; set; }

    [Parameter]
    public EventCallback<TrackVo> SelectedTrackChanged { get; set; }

    [Parameter]
    public EventCallback<TrackVo> OnTrackDelete { get; set; }
    
    [CascadingParameter(Name = "PopoverConfirmButtonCloseAsync")]
    private Func<Task>? OnCloseAsync { get; set; }

    private string? CreateStyle(TrackVo trackVo)
    {
        return CssBuilder.Default("display: flex;")
            .AddClass("color: var(--bb-header-bg);", SelectedTrack == trackVo)
            .Build();
    }

    private void SelectChanged(TrackVo trackVo)
    {
        SelectedTrack = trackVo;
        SelectedTrackChanged.InvokeAsync(SelectedTrack);
    }

    private async Task DeleteTrack(TrackVo trackVo)
    {
        await OnTrackDelete.InvokeAsync(trackVo);
    }

    private async Task CreatePlaylist()
    {
        if (OnCloseAsync != null)
        {
            await OnCloseAsync();
        }
        
        var playlist = new PlaylistVo();
        playlist.Songs = Tracks;
        await DialogService.Show(new DialogOption()
        {
            BodyTemplate = BootstrapDynamicComponent.CreateComponent<PlaylistDialog>(new Dictionary<string, object?>()
            {
                [nameof(PlaylistDialog.Playlist)]  = playlist
            }).Render(),
            Title = StringLocalizer["CreatePlaylist"],
            ShowFooter = false
        });
    }

}